# -*- coding: utf-8 -*-
import sys
from resource_management import *
from resource_management.core.logger import Logger
from resource_management.libraries.functions import default
from resource_management.core.resources.system import Execute
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.libraries.script.script import Script
import commands

Logger.initialize_logger()
reload(sys)
sys.setdefaultencoding('utf-8')

# server configurations
# 这个要先定义,读取configuration下的配置文件
config = Script.get_config()
print(str(config))


# package download url
download_url = config['configurations']['dolphin-ambari-config']['dolphinscheduler.package.download.url']
#download_url = '''http://173.18.12.1/common_pkgs/dolphin/apache-dolphinscheduler-incubating-1.3.5-dolphinscheduler-bin.tar.gz'''
jdbc_driver_download_url = config['configurations']['dolphin-ambari-config']['jdbc.connector.download.url']
#jdbc_driver_download_url = '''http://173.18.12.1/common_pkgs/mysql_pkgs/mysql-connector-java-5.1.27.jar'''
# hdp info
cmd = "/usr/bin/hdp-select versions"
hdp_version = commands.getoutput(cmd)
hdp_base_dir = '/usr/hdp/' + hdp_version

dolphin_home = hdp_base_dir + "/dolphinscheduler"

# conf_dir = "/etc/"
dolphin_conf_dir = dolphin_home+"/conf"
hdp_dolphin_log_dir = dolphin_home+"/logs"
dolphin_log_dir = "/var/log/dolphinscheduler"
dolphin_bin_dir = dolphin_home+"/bin"
dolphin_lib_jars = dolphin_home+"/lib/*"
dolphin_pidfile_dir = "/var/run/dolphinscheduler"

# dolphin-env
dolphin_env_map = {}
# 通过config函数将dolphin-env.xml的内容解析成字典,写入dolphin_env_map中
dolphin_env_map.update(config['configurations']['dolphin-env'])
dolphin_user = dolphin_env_map['dolphin.user']
dolphin_group = dolphin_env_map['dolphin.group']
java_path = config['configurations']['dolphin-env']['java_home'] + '/bin/java'

# .dolphinscheduler_env.sh
dolphin_env_path = dolphin_conf_dir + '/env/dolphinscheduler_env.sh'
dolphin_env_content = dolphin_env_map['dolphinscheduler-env-content']

# datasource.properties
dolphin_datasource_map = {}
dolphin_datasource_map.update(config['configurations']['dolphin-datasource'])

# master.properties
dolphin_master_map ={}
dolphin_master_map = config['configurations']['dolphin-master']

# worker.properties
# 获取dolphin-worker.xml里的内容,这里改动了,增加了空字典
dolphin_worker_map = {}
dolphin_worker_map.update(config['configurations']['dolphin-worker'])

# application-api.properties
dolphin_app_api_map = {}
dolphin_app_api_map.update(config['configurations']['dolphin-application-api'])

# common.properties
dolphin_common_map = {}
dolphin_common_map.update(config['configurations']['dolphin-common'])

# application-alert.properties
dolphin_alert_map = {}
wechat_push_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$token'
wechat_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpId&corpsecret=$secret'
wechat_team_send_msg = '{\"toparty\":\"$toParty\",\"agentid\":\"$agentId\",\"msgtype\":\"text\",\"text\":{\"content\":\"$msg\"},\"safe\":\"0\"}'
wechat_user_send_msg = '{\"touser\":\"$toUser\",\"agentid\":\"$agentId\",\"msgtype\":\"markdown\",\"markdown\":{\"content\":\"$msg\"}}'

dolphin_alert_config_map = config['configurations']['dolphin-alert']

if dolphin_alert_config_map['enterprise.wechat.enable']:
    dolphin_alert_map['enterprise.wechat.push.ur'] = wechat_push_url
    dolphin_alert_map['enterprise.wechat.token.url'] = wechat_token_url
    dolphin_alert_map['enterprise.wechat.team.send.msg'] = wechat_team_send_msg
    dolphin_alert_map['enterprise.wechat.user.send.msg'] = wechat_user_send_msg

dolphin_alert_map.update(dolphin_alert_config_map)

# quartz.properties
dolphin_quartz_map = {}
dolphin_quartz_map.update(config['configurations']['dolphin-quartz'])
dolphin_quartz_map.update(config['configurations']['dolphin-zookeeper'])

# zookeeper.properties
dolphin_zookeeper_map = {}
dolphin_zookeeper_map.update(config['configurations']['dolphin-zookeeper'])

# log config content
worker_logback = config['configurations']['dolphin-logback']['worker_logback.xml']
master_logback = config['configurations']['dolphin-logback']['master_logback.xml']
apiserver_logback = config['configurations']['dolphin-logback']['apiserver_logback.xml']
alert_logback = config['configurations']['dolphin-logback']['alert_logback.xml']
# 然后j2文件读取,生成正式的属性文件